home *** CD-ROM | disk | FTP | other *** search
- Path: locutus.rchland.ibm.com!usenet
- From: pstaite@vnet.ibm.com
- Newsgroups: comp.lang.c++
- Subject: Re: Initalizing variables in a for loop
- Date: 16 Jan 1996 15:27:01 GMT
- Organization: IBM OS/2 Device Driver Development Rochester, MN
- Message-ID: <4dgg45$11p2@locutus.rchland.ibm.com>
- References: <st95h6e9-1501962022390001@myers1-006.resnet.drexel.edu>
- Reply-To: pstaite@vnet.ibm.com
- NNTP-Posting-Host: warpone.rchland.ibm.com
- X-Newsreader: IBM NewsReader/2 v1.2
-
- In <st95h6e9-1501962022390001@myers1-006.resnet.drexel.edu>, st95h6e9@dunx1.ocs.drexel.edu (Luke Cassady-Dorion) writes:
- >When initalizing a variable in a for loop as in the example below does the
- >compiler free up the memory allocated to that variable after the loop
- >exits, thus making the variable unuseable by other parts of the function,
- >or its it retined??
- >
- >
- > for (int i=0; i < some_value, i++)
-
- Yes and no. :-) (don't you hate answers like that?)
-
- In "classic" C++ the int i would be available from within the for loop
- on until the end of the scope that enclosed the for loop. That is,
- this would be legal:
-
- #include<iostream.h>
- int main( int argc, char* argv[] ) {
- for( int i = 0 ; i < argc ; ++i )
- if( argv[ i ][ 0 ] == '-' )
- break;
- cout << "First option is argument " << i << endl;
- return 0; }
-
- Note the use of i after the for loop. However, the draft Ansi C++ Spec
- changes the scope of an object declared in the init statement of a for
- loop to be just within the for loop. This means the example above would
- get an error on the i variable in the cout statement.
-
- So far, the only compiler I know of that has implemented this is the
- latest version of g++. Look for it to come to a compiler near you soon,
- and break some code...
-
-
- Phil Staite, team OS/2
- internet: pstaite@vnet.ibm.com internal: pstaite@rchland
-
-